﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using Database;
using System.Timers;
using Server.Models;

namespace Server
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class WebApiApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            GalaxyUpdate();
            
        }

        public static void GalaxyUpdate()
        {
            try
            {
                var urllist = GalaxyCinema.LayURL(0);
                var list1 = GalaxyCinema.DsThongTinFilm(urllist);
                urllist = GalaxyCinema.LayURL(1);
                var list2 = GalaxyCinema.DsThongTinFilm(urllist);
                foreach (var i in list2)
                {
                    list1.Add(i);
                }
                foreach (var f in list1)
                {
                    GalaxyData.UpdateFilm(f);
                }
            }
            catch (Exception ex)
            { }
            
            var time = new Timer(1000*60*60*12); // Set up the timer for 3 seconds
            //
            // Type "_timer.Elapsed += " and press tab twice.
            //
            
            time.Elapsed += new ElapsedEventHandler(_timer_Elapsed);
            time.Enabled = true; // Enable it
        }
        static void _timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            try
            {
                var urllist = GalaxyCinema.LayURL(0);
                var list1 = GalaxyCinema.DsThongTinFilm(urllist);
                urllist = GalaxyCinema.LayURL(1);
                var list2 = GalaxyCinema.DsThongTinFilm(urllist);
                foreach (var i in list2)
                {
                    list1.Add(i);
                }
                foreach (var f in list1)
                {
                    GalaxyData.UpdateFilm(f);
                }
            }
            catch (Exception ex)
            { }
        }
       
    }
}